//
//  CourseViewController.m
//  moon
//
//  Created by beyself on 16/6/18.
//  Copyright © 2016年 beyself. All rights reserved.
//
#import "Utils.h"
#import "CourseViewController.h"

@interface CourseViewController ()

@end

@implementation CourseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = [NSString stringWithFormat:@"%@课程表",_className];
    self.view.backgroundColor = WHITE;
    
    NSDate* date = [NSDate date];
    NSCalendar* calendar = [NSCalendar currentCalendar];
    NSDateComponents* components = [calendar components:NSWeekOfYearCalendarUnit  fromDate:date];
    int week = [components weekOfYear];
    
    UILabel* label = [[UILabel alloc] init];
    label.frame = CGRectMake(0, 0, SCREEN_WIDTH, 32);
    label.backgroundColor = BLUE;
    label.font = FONT_14;
    label.textColor = ORANGE;
    label.text = [NSString stringWithFormat:@"%@ 第%d周",[Utils date2String:date format:@"yy-MM-dd"],week];
    label.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:label];
    
    int width = (SCREEN_WIDTH - 30)/5;
    for(int i = 0; i < 5;i++)
    {
        label = [[UILabel alloc] init];
        label.frame = CGRectMake(30 + i*width, 32, width, 30);
        label.textColor = BLACK;
        label.font = FONT_14;
        label.textAlignment = NSTextAlignmentCenter;
        if(i == 0) label.text = @"周一";
        if(i == 1) label.text = @"周二";
        if(i == 2) label.text = @"周三";
        if(i == 3) label.text = @"周四";
        if(i == 4) label.text = @"周五";
        [self.view addSubview:label];
    }
    
    CALayer* layer = [[CALayer alloc] init];
    layer.frame = CGRectMake(0, 62, SCREEN_WIDTH, .5);
    layer.backgroundColor = LINE.CGColor;
    [self.view.layer addSublayer:layer];
    
    
    NSMutableDictionary* param = [[NSMutableDictionary alloc] init];
    [param setValue:[NSNumber numberWithBool:_classId] forKey:@"class_id"];
    [Utils sendHttpPost:param method:@"get_schedule_by_class_id" controller:self flag:0];
}

- (void) httpCallback:(NSDictionary*)data flag:(int)flag
{
    NSDate* date = [NSDate date];
    NSCalendar* calendar = [NSCalendar currentCalendar];
    NSDateComponents* components = [calendar components:NSCalendarUnitWeekday  fromDate:date];
    int week = [components weekday] - 1;
    
    NSArray* array = data[@"data"][@"content"];
    NSString* remark = data[@"data"][@"remark"];
    remark = [remark stringByReplacingOccurrencesOfString:@"\r\n" withString:@" "];
    
    int width = (SCREEN_WIDTH - 30)/5;
    int height = (SCREEN_HEIGHT - 20 - 62)/8;
    
    for(int i = 0; i < 8; i++)
    {
        NSArray* item = array[i];
        for(int j = 0; j < 6;j++)
        {
            UILabel* label = [[UILabel alloc] init];
            label.frame = CGRectMake(j==0?0:30 + (j - 1)*width, i < 4?62 + height*i:82 + height*i, j==0?30:width, height);
            label.textAlignment = NSTextAlignmentCenter;
            label.textColor = GRAY;
            label.font = FONT_12;
            label.numberOfLines = 0;
            label.lineBreakMode = NSLineBreakByWordWrapping;
            if(j > 0) label.text = item[j - 1];
            else label.text = [NSString stringWithFormat:@"%d",i + 1];
            [self.view addSubview:label];
            
            CALayer* layer = [[CALayer alloc] init];
            layer.frame = CGRectMake(0, 0, .5, height);
            layer.backgroundColor = LINE.CGColor;
            [label.layer addSublayer:layer];
            
            layer = [[CALayer alloc] init];
            layer.frame = CGRectMake(0, height - .5, width, .5);
            layer.backgroundColor = LINE.CGColor;
            [label.layer addSublayer:layer];
            
            if(i == 4)
            {
                layer = [[CALayer alloc] init];
                layer.frame = CGRectMake(0, 0, width, .5);
                layer.backgroundColor = LINE.CGColor;
                [label.layer addSublayer:layer];
            }
            
            if(week != 0 && week == j)
            {
                label.textColor = WHITE;
                label.backgroundColor = GREEN;
            }
        }
    }
    
    UILabel* label = [[UILabel alloc] init];
    label.frame = CGRectMake(0, 62 + 4*height, SCREEN_WIDTH, 20);
    label.textAlignment = NSTextAlignmentCenter;
    label.textColor = BLUE;
    label.font = FONT_12;
    label.text = remark;
    [self.view addSubview:label];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
@end
